import axios from 'axios'
const baseURL = window.g.api
import store from '../store'
const service = axios.create({
    baseURL: baseURL,
    timeout: 15000, // 请求超时时间
    // headers:{
    //   'Content-Type':'application/x-www-form-urlencoded',
    //   token : sessionStorage.getItem('token')
    // },
})
service.interceptors.request.use(config => {
  console.log('config: ', config);
    config.headers.token = sessionStorage.getItem('token')
    // if (config.url == '/param/add') {
    //   config.headers['Content-Type'] = 'Content-Type: application/json'
    // }else {
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    // }
    return config
}, error => {
    console.error(error, '请求拦截err')
    Promise.reject(error)
})
service.interceptors.response.use(
    response => {
        console.log(response,'response 请求返回')
        const res = response.data;
        if (res == '1000' || response.status == '200') {
            return res;
        }
        if (res == '100') {
            return res.info;
        } else if (res == "406") { //406登录超时
          this.$message({
                showClose: true,
                message: res.msg,
                type: 'error',
                duration: 500,
                onClose: () => {
                    store.dispatch('FedLogOut').then(() => {
                        location.reload()// 为了重新实例化vue-router对象 避免bug
                    })
                }
            });
            return Promise.reject("未登录")
        } else {
          this.$message({
                message: res.msg,
                type: 'error',
                duration: 3 * 1000
            })
            return Promise.reject(res)
        }
    },
    error => {
        console.error('err' + error)// for debug
        this.$message({
            message: error.message,
            type: 'error',
            duration: 3 * 1000
        })
        return Promise.reject(error)
    }
)


export default service